﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Input;
using BindingTargetUpdatedEventTrigger.Model;

namespace BindingTargetUpdatedEventTrigger.ViewModel
{
    public class CustomValueViewModel : INotifyPropertyChanged 
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private readonly CustomValue _customValue;

        private RelayCommand _updateCommand;

        public CustomValueViewModel(CustomValue customValue)
        {
            _customValue = customValue;
        }

        public double Value
        {
            get { return _customValue.Value; }
            private set
            {
                _customValue.Value = value;
                OnPropertyChanged("Value");
            }
        }

        public ICommand UpdateCommand
        {
            get
            {
                if (_updateCommand == null)
                {
                    _updateCommand = new RelayCommand(
                        param => Update(),
                        param => CanUpdate
                        );
                }
                return _updateCommand;
            }
        }

        protected bool CanUpdate
        {
            get { return true; }
        }

        private void Update()
        {
            Value++;
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
